﻿using System;
using System.Collections.ObjectModel;
using System.IO;
using System.Text.Json;
using UIWindows.Models;

namespace UIWindows.GlobalStatics
{
    [Attributes.Singleton]
    public class DisplayItemsStatic
    {
        /// <summary>
        /// 主页是否更新列表
        /// </summary>
        public bool IsHomeUpdate { get; set; }
        /// <summary>
        /// 数据页面是否更新列表
        /// </summary>
        public bool IsDataUpdate { get; set; }
        public ObservableCollection<RoleInitModel> RoleInit { get; set; }
        public ObservableCollection<DisplayItemsModel> WeighingDisplayItems { get; set; }

        public ObservableCollection<DisplayItemsModel> CoilCoreLoadingDataDisplayItems { get; set; }
        public ObservableCollection<DisplayItemsModel> CupSleeveChangingDataDisplayItems { get; set; }
        public ObservableCollection<DisplayItemsModel> PoleEarFoldingDataDisplayItems { get; set; }
        public ObservableCollection<DisplayItemsModel> ShellInsertionDataDisplayItems { get; set; }
        public ObservableCollection<DisplayItemsModel> SpotoWeldDataDisplayItems { get; set; }
        public ObservableCollection<DisplayItemsModel> GrooveRollingMachineDataDisplayItems { get; set; }
        public ObservableCollection<DisplayItemsModel> ShortCircuitTesterDataDisplayItems { get; set; }
        public ObservableCollection<DisplayItemsModel> TrayFillerDataDisplayItems { get; set; }
        
        
        public ObservableCollection<DisplayItemsModel> DismantlingDiscDataDisplayItems { get; set; }
        public ObservableCollection<DisplayItemsModel> CapWeldingDataDisplayItems { get; set; }
        public ObservableCollection<DisplayItemsModel> SealingMachineDataDisplayItems { get; set; }



        public ObservableCollection<DisplayItemsModel> PlateDataDisplayItems { get; set; }
        public ObservableCollection<DisplayItemsModel> CleaningDataDisplayItems { get; set; }
        public ObservableCollection<DisplayItemsModel> MantleDataDisplayItems { get; set; }
        public ObservableCollection<DisplayItemsModel> MarkingDataDisplayItems { get; set; }

        /// <summary>
        /// 是否显示短路测试数据
        /// </summary>
        public bool IsShowShortCircuit { get; set; } = true;



        public DisplayItemsStatic()
        {
            IsHomeUpdate = true;
            IsDataUpdate = true;
            if (!File.Exists(GlobalStatic.RoleInitPath))
            {
                RoleInit = new ObservableCollection<RoleInitModel>();
            }
            else
            {
                try
                {
                    RoleInit = JsonSerializer.Deserialize<ObservableCollection<RoleInitModel>>(File.ReadAllText(GlobalStatic.RoleInitPath), GlobalStatic.Options);
                }
                catch (Exception)
                {
                    RoleInit = new ObservableCollection<RoleInitModel>();
                }
            }
            if (!File.Exists(GlobalStatic.DisplayItemPath))
            {
                WeighingDisplayItems = new ObservableCollection<DisplayItemsModel>();
                CoilCoreLoadingDataDisplayItems = new ObservableCollection<DisplayItemsModel>();
                CupSleeveChangingDataDisplayItems = new ObservableCollection<DisplayItemsModel>();
                PoleEarFoldingDataDisplayItems = new ObservableCollection<DisplayItemsModel>();

                ShellInsertionDataDisplayItems = new ObservableCollection<DisplayItemsModel>();
                SpotoWeldDataDisplayItems = new ObservableCollection<DisplayItemsModel>();
                GrooveRollingMachineDataDisplayItems = new ObservableCollection<DisplayItemsModel>();
                ShortCircuitTesterDataDisplayItems = new ObservableCollection<DisplayItemsModel>();

                TrayFillerDataDisplayItems = new ObservableCollection<DisplayItemsModel>();
                PlateDataDisplayItems = new ObservableCollection<DisplayItemsModel>();
                CleaningDataDisplayItems = new ObservableCollection<DisplayItemsModel>();
                MantleDataDisplayItems = new ObservableCollection<DisplayItemsModel>();
                MarkingDataDisplayItems = new ObservableCollection<DisplayItemsModel>();

                DismantlingDiscDataDisplayItems = new ObservableCollection<DisplayItemsModel>();
                CapWeldingDataDisplayItems = new ObservableCollection<DisplayItemsModel>();
                SealingMachineDataDisplayItems = new ObservableCollection<DisplayItemsModel>();
            }
            else
            {
                try
                {
                    WeighingDisplayItems = JsonSerializer.Deserialize<ObservableCollection<DisplayItemsModel>>(File.ReadAllText(GlobalStatic.DisplayItemPath), GlobalStatic.Options);

                    CoilCoreLoadingDataDisplayItems = JsonSerializer.Deserialize<ObservableCollection<DisplayItemsModel>>(File.ReadAllText(GlobalStatic.CoilCoreLoadingDataDisplayItemPath), GlobalStatic.Options);
                    CupSleeveChangingDataDisplayItems = JsonSerializer.Deserialize<ObservableCollection<DisplayItemsModel>>(File.ReadAllText(GlobalStatic.CupSleeveChangingDataDisplayItemPath), GlobalStatic.Options);
                    PoleEarFoldingDataDisplayItems = JsonSerializer.Deserialize<ObservableCollection<DisplayItemsModel>>(File.ReadAllText(GlobalStatic.PoleEarFoldingDataDisplayItemPath), GlobalStatic.Options);

                    ShellInsertionDataDisplayItems = JsonSerializer.Deserialize<ObservableCollection<DisplayItemsModel>>(File.ReadAllText(GlobalStatic.ShellInsertionDataDisplayItemPath), GlobalStatic.Options);
                    SpotoWeldDataDisplayItems = JsonSerializer.Deserialize<ObservableCollection<DisplayItemsModel>>(File.ReadAllText(GlobalStatic.SpotoWeldDataDisplayItemPath), GlobalStatic.Options);
                    GrooveRollingMachineDataDisplayItems = JsonSerializer.Deserialize<ObservableCollection<DisplayItemsModel>>(File.ReadAllText(GlobalStatic.GrooveRollingMachineDataDisplayItemPath), GlobalStatic.Options);
                    ShortCircuitTesterDataDisplayItems = JsonSerializer.Deserialize<ObservableCollection<DisplayItemsModel>>(File.ReadAllText(GlobalStatic.ShortCircuitTesterDataDisplayItemPath), GlobalStatic.Options);
                    TrayFillerDataDisplayItems = JsonSerializer.Deserialize<ObservableCollection<DisplayItemsModel>>(File.ReadAllText(GlobalStatic.TrayFillerDataDisplayItemsPath), GlobalStatic.Options);
                    PlateDataDisplayItems = JsonSerializer.Deserialize<ObservableCollection<DisplayItemsModel>>(File.ReadAllText(GlobalStatic.PlateDataDisplayItemsPath), GlobalStatic.Options);


                    CleaningDataDisplayItems = JsonSerializer.Deserialize<ObservableCollection<DisplayItemsModel>>(File.ReadAllText(GlobalStatic.CleaningDataDisplayItemsPath), GlobalStatic.Options);
                    MantleDataDisplayItems = JsonSerializer.Deserialize<ObservableCollection<DisplayItemsModel>>(File.ReadAllText(GlobalStatic.MantleDataDisplayItemsPath), GlobalStatic.Options);
                    MarkingDataDisplayItems = JsonSerializer.Deserialize<ObservableCollection<DisplayItemsModel>>(File.ReadAllText(GlobalStatic.MarkingDataDisplayItemsPath), GlobalStatic.Options);


                    DismantlingDiscDataDisplayItems = JsonSerializer.Deserialize<ObservableCollection<DisplayItemsModel>>(File.ReadAllText(GlobalStatic.DismantlingDiscDataDisplayItemsPath), GlobalStatic.Options);
                    CapWeldingDataDisplayItems = JsonSerializer.Deserialize<ObservableCollection<DisplayItemsModel>>(File.ReadAllText(GlobalStatic.CapWeldingDataDisplayItemsPath), GlobalStatic.Options);
                    SealingMachineDataDisplayItems = JsonSerializer.Deserialize<ObservableCollection<DisplayItemsModel>>(File.ReadAllText(GlobalStatic.SealingMachineDataDisplayItemsPath), GlobalStatic.Options);
               
                
                
                }
                catch (Exception)
                {
                    WeighingDisplayItems = new ObservableCollection<DisplayItemsModel>();
                    CoilCoreLoadingDataDisplayItems = new ObservableCollection<DisplayItemsModel>();
                    CupSleeveChangingDataDisplayItems = new ObservableCollection<DisplayItemsModel>();
                    PoleEarFoldingDataDisplayItems = new ObservableCollection<DisplayItemsModel>();

                    ShellInsertionDataDisplayItems = new ObservableCollection<DisplayItemsModel>();
                    SpotoWeldDataDisplayItems = new ObservableCollection<DisplayItemsModel>();
                    GrooveRollingMachineDataDisplayItems = new ObservableCollection<DisplayItemsModel>();
                    ShortCircuitTesterDataDisplayItems = new ObservableCollection<DisplayItemsModel>();
                    TrayFillerDataDisplayItems = new ObservableCollection<DisplayItemsModel>();

                    PlateDataDisplayItems = new ObservableCollection<DisplayItemsModel>();
                    CleaningDataDisplayItems = new ObservableCollection<DisplayItemsModel>();
                    MantleDataDisplayItems = new ObservableCollection<DisplayItemsModel>();
                    MarkingDataDisplayItems = new ObservableCollection<DisplayItemsModel>();

                    DismantlingDiscDataDisplayItems = new ObservableCollection<DisplayItemsModel>();
                    CapWeldingDataDisplayItems = new ObservableCollection<DisplayItemsModel>();
                    SealingMachineDataDisplayItems = new ObservableCollection<DisplayItemsModel>();

                }
            }
            if (File.Exists(GlobalStatic.DisplayItemPath2))
            {
                try
                {
                    IsShowShortCircuit = JsonSerializer.Deserialize<bool>(File.ReadAllText(GlobalStatic.DisplayItemPath2), GlobalStatic.Options);
                }
                catch (Exception)
                {
                    IsShowShortCircuit = true;
                }
            }
        }

        public void SaveRoleInit() => File.WriteAllText(GlobalStatic.RoleInitPath, JsonSerializer.Serialize(RoleInit, GlobalStatic.Options));

        public void SaveDisplayItem()
        {
            File.WriteAllText(GlobalStatic.DisplayItemPath, JsonSerializer.Serialize(WeighingDisplayItems, GlobalStatic.Options));
            File.WriteAllText(GlobalStatic.CoilCoreLoadingDataDisplayItemPath, JsonSerializer.Serialize(CoilCoreLoadingDataDisplayItems, GlobalStatic.Options));
            File.WriteAllText(GlobalStatic.CupSleeveChangingDataDisplayItemPath, JsonSerializer.Serialize(CupSleeveChangingDataDisplayItems, GlobalStatic.Options));
            File.WriteAllText(GlobalStatic.PoleEarFoldingDataDisplayItemPath, JsonSerializer.Serialize(PoleEarFoldingDataDisplayItems, GlobalStatic.Options));

            File.WriteAllText(GlobalStatic.ShellInsertionDataDisplayItemPath, JsonSerializer.Serialize(ShellInsertionDataDisplayItems, GlobalStatic.Options));
            File.WriteAllText(GlobalStatic.SpotoWeldDataDisplayItemPath, JsonSerializer.Serialize(SpotoWeldDataDisplayItems, GlobalStatic.Options));
            File.WriteAllText(GlobalStatic.GrooveRollingMachineDataDisplayItemPath, JsonSerializer.Serialize(GrooveRollingMachineDataDisplayItems, GlobalStatic.Options));
            File.WriteAllText(GlobalStatic.ShortCircuitTesterDataDisplayItemPath, JsonSerializer.Serialize(ShortCircuitTesterDataDisplayItems, GlobalStatic.Options));
            File.WriteAllText(GlobalStatic.TrayFillerDataDisplayItemsPath, JsonSerializer.Serialize(TrayFillerDataDisplayItems, GlobalStatic.Options));
            File.WriteAllText(GlobalStatic.PlateDataDisplayItemsPath, JsonSerializer.Serialize(PlateDataDisplayItems, GlobalStatic.Options));
           
            File.WriteAllText(GlobalStatic.CleaningDataDisplayItemsPath, JsonSerializer.Serialize(CleaningDataDisplayItems, GlobalStatic.Options));
            File.WriteAllText(GlobalStatic.MantleDataDisplayItemsPath, JsonSerializer.Serialize(MantleDataDisplayItems, GlobalStatic.Options));
            File.WriteAllText(GlobalStatic.MarkingDataDisplayItemsPath, JsonSerializer.Serialize(MarkingDataDisplayItems, GlobalStatic.Options));
            
            File.WriteAllText(GlobalStatic.DismantlingDiscDataDisplayItemsPath, JsonSerializer.Serialize(DismantlingDiscDataDisplayItems, GlobalStatic.Options));
            File.WriteAllText(GlobalStatic.CapWeldingDataDisplayItemsPath, JsonSerializer.Serialize(CapWeldingDataDisplayItems, GlobalStatic.Options));
            File.WriteAllText(GlobalStatic.SealingMachineDataDisplayItemsPath, JsonSerializer.Serialize(SealingMachineDataDisplayItems, GlobalStatic.Options));



            File.WriteAllText(GlobalStatic.DisplayItemPath2, JsonSerializer.Serialize(IsShowShortCircuit, GlobalStatic.Options));
        }
    }
}
